//
//  ViewController.m
//  UI14-作业（礼物说）
//
//  Created by 韩旭 on 16/8/2.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ViewController.h"
#import "HX_CollectionViewTitle.h"
#import "HX_CollectionCactoryTItle.h"
#define kCollection @"resue"
#define kCollectionTitle @"title"
#define kCollectionCacory @"titleCatary"
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collecctionView;
@property (nonatomic, strong) UIView *viewBack;
@property (nonatomic, strong) UITextField *filed;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"礼物说";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.viewBack = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 100)];
    _viewBack.backgroundColor = [UIColor whiteColor];
    self.filed = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, kWidth - 10, 20)];
    self.filed.placeholder = @"搜索一份七夕礼物送个，ta吧";
    self.filed.textAlignment = 1;
    _filed.backgroundColor = [UIColor lightGrayColor];
    _filed.layer.cornerRadius = 10;
    [self.viewBack addSubview:_filed];
    [self.view addSubview:_viewBack];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((CGRectGetWidth(self.view.bounds) - 30) / 2, 100);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.headerReferenceSize = CGSizeMake(0, 50);
    _collecctionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:layout];
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    [self.view addSubview:_collecctionView];
    _collecctionView.delegate = self;
    _collecctionView.dataSource = self;
    _collecctionView.backgroundColor = [UIColor whiteColor];
    [_collecctionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollection];
    [_collecctionView registerClass:[HX_CollectionViewTitle class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionTitle];
    
    [_collecctionView registerClass:[HX_CollectionCactoryTItle class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionCacory];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollection forIndexPath:indexPath];
    cell.backgroundColor = COLORAUTO;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(0, 400);
    } else {
        return CGSizeMake(0, 30);
    }
    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HX_CollectionViewTitle *resue = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionTitle forIndexPath:indexPath];
        resue.backgroundColor = [UIColor whiteColor];
        return resue;
    } else {
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            HX_CollectionCactoryTItle *cell = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionCacory forIndexPath:indexPath];
            cell.backgroundColor = [UIColor whiteColor];
            return cell;
            
        } else {
            return nil;
        };
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
